package Search;

import Data.ChatData;

import java.sql.*;

public class AddFriend {
    private ChatData chatData;

    String url = "jdbc:mysql://127.0.0.1:3306/nox?useUnicode=true&characterEncoding=UTF-8&userSSL=false&serverTimezone=GMT%2B8";
    String user = "root";
    String password = "Mi20030610.";

    public AddFriend(ChatData chatData) {
        this.chatData = chatData;
    }
    public boolean DeleteFriendTosql() throws ClassNotFoundException, SQLException {
        String sql="DELETE FROM friendlist WHERE (userId=? and userFId=?) or (userId=? and userFId=?);";
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);

        PreparedStatement operation=null;
        operation=Conn.prepareStatement(sql);
        operation.setString(1,chatData.getSender_id());
        operation.setString(2,chatData.getReceiver_id());
        operation.setString(3,chatData.getReceiver_id());
        operation.setString(4,chatData.getSender_id());
        int k=operation.executeUpdate();
        if(k>0)
        {
            //删除成功
            return true;
        }
        else
        {
            return false;
            //删除失败
        }
    }

    public boolean UpdateFriendTosql() throws ClassNotFoundException, SQLException {
        //对方同意好友请求
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        DeleteFriendTosql();
        //代表已经是好友了
        String sql="INSERT INTO friendlist (userId,userFId,isfriend) VALUES(?,?,1);";
        PreparedStatement operation=null;
        operation=Conn.prepareStatement(sql);
        operation.setString(1,chatData.getSender_id());
        operation.setString(2,chatData.getReceiver_id());
        int k=operation.executeUpdate();
        operation.close();
        Conn.close();
        if(k>0)
        {
            //更新成功
            //需要刷新俩边的好友列表

            return true;
        }
        else
        {
            return false;
        }
    }
    public boolean InsertFriendTosql() throws ClassNotFoundException, SQLException {

        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection Conn = DriverManager.getConnection(url, user, password);
        PreparedStatement operation=null;
        //应该先判断是否已经有
        String presql="SELECT *FROM friendlist where (userId=? and userFId=? ) or (userId=? and userFId=? );";
        operation=Conn.prepareStatement(presql);
        operation.setString(1,chatData.getSender_id());
        operation.setString(2,chatData.getReceiver_id());
        operation.setString(3,chatData.getReceiver_id());
        operation.setString(4,chatData.getSender_id());
        ResultSet resultSet=operation.executeQuery();
        if(resultSet.next())
        {
            //已经有这一条消息
            //不能多次请求
            operation.close();
            Conn.close();
            return true;
        }


        String sql="INSERT INTO friendlist (userId,userFId,isfriend) VALUES(?,?,2);";
        operation=null;
        operation=Conn.prepareStatement(sql);
        operation.setString(1,chatData.getSender_id());
        operation.setString(2,chatData.getReceiver_id());

        int k=operation.executeUpdate();
        if(k>0)
        {
            //说明成功,显示一个发送请求成功
            operation.close();
            Conn.close();
            return true;
        }
        else
        {
            //不成功
            operation.close();
            Conn.close();
            return false;
        }
    }
}
